package com.yc.mvc.web;

import com.yc.mvc.uitl.VerifyCodeUtils;
import com.yc.mvc.web.bean.Result;
import com.yc.mvc.web.bean.User;
import org.springframework.http.MediaType;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
public class IndexDo {

    @RequestMapping(path = "vcode",
            produces = MediaType.IMAGE_PNG_VALUE)
    public void vcode(HttpServletResponse response,
                      HttpSession session) throws IOException {
        final String code = VerifyCodeUtils.outputImage(response);
        session.setAttribute("vcode", code);
    }

    @RequestMapping(value = "register",
        produces = MediaType.TEXT_HTML_VALUE + ";charset=utf-8")
    public String register(User user,
                         @SessionAttribute String vcode,
                         String rcode){
        if(rcode == null){
            return "请输入验证码!";
        } else if( rcode.equals(vcode)){
            return "注册成功!";
        } else {
            return "验证码错误!";
        }

    }

    @RequestMapping("concat")
    public String concat(@RequestParam(name="x", required = true) String a,
                         @RequestParam(value="y", defaultValue = "zhangsan") String b){
        return a + b;
    }

    // add?a=1&b=2
    // add/1/2  => RESTful
    @RequestMapping("add/{b}/{a}")
    public int add(@PathVariable int a,
                   @PathVariable("b") int c){
        return a + c;
    }

    @RequestMapping("showHeader")
    public Map<String,String> showHeader(
            HttpServletRequest request,
            @RequestHeader String Accept,
            @RequestHeader("Accept-Encoding") String acceptEncoding,
            @RequestHeader(defaultValue = "abc") String Cookie,
            @CookieValue String name,
            @CookieValue(value = "password", defaultValue = "123") String pwd
    ){
        final String host = request.getHeader("Host");
        Map<String,String> ret = new HashMap<>();
        ret.put("host", host);
        ret.put("Accept-Encoding", acceptEncoding);
        ret.put("Cookie", Cookie);
        ret.put("name", name);
        ret.put("password", pwd);
        return ret;
    }


    @RequestMapping("register.do")
    public Result reg(@RequestBody @Valid User user, Errors errors){
        System.out.println(user);
        if (errors.hasErrors()) {
            return new Result(0, "验证错误",
                    errors.getAllErrors());
        } else {
            return new Result(1, "注册成功!", null);
        }
    }

    @PostMapping("upload")
    public Result upload(@RequestParam("file") MultipartFile file)
            throws IOException {
        String filename = file.getOriginalFilename();
        String suffix = filename.replaceAll(".+(\\.\\w+)","$1");
        // 随机生成文件名 + 源文件后缀
        filename = UUID.randomUUID().toString() + suffix;
        File fileObj = new File("F:\\_文件柜\\133\\1117", filename);
        file.transferTo(fileObj);
        String webPath = "upload/" + filename;
        return new Result(1, "文件上传成功!", webPath);

    }









}
